function [ output ] = apply_gammagamma( variance , input )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

alpha = 1 + variance*10;
a=floor(alpha);
n=length(input);
u=rand(a,n);

if (a>1)
    y=sum(-log(u))*alpha/a;
else
    y=-log(u)*alpha/a;
end;

u1=rand(n,1);

pv=1.0;
k=1;
for i=1:n
    if (u1(i)<(exp(1)*y(i)*exp(-y(i)/alpha)/alpha)^(alpha-a))
        x1(k)=y(i);
        k=k+1;
    end;
        
    if (i>1)
        pv=x2(i-1);
    end;
    
    thred=(y(i)/pv*exp((pv-y(i))/alpha))^(alpha-a);
    if (u1(i)<thred)
        x2(i)=y(i);
    else
        x2(i)=pv;
    end;
end;

output = x2;

end

